package com.cf.oss.core;

import com.aliyun.oss.model.Callback;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.UploadFileRequest;

import java.util.LinkedList;
import java.util.List;

/**
 * game-server-BatchUploadFileRequests
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/5 下午 12:01
 */
public class BatchUploadFileRequests {
    private List<UploadFileRequest> uploadFileRequests;
    private String bucketName;

    private BatchUploadFileRequests(String bucketName) {
        Assert.hasLength(bucketName, "Parameter 'bucketName' must not be empty.");
        this.bucketName = bucketName;
    }

    public static BatchUploadFileRequests start(String bucketName) {
        return new BatchUploadFileRequests(bucketName);
    }

    public EntryBuilder with(String key) {
        return new EntryBuilder(this, this.bucketName, key);
    }

    public List<UploadFileRequest> get() {
        return this.uploadFileRequests;
    }

    private void add(UploadFileRequest uploadFileRequest) {
        Assert.notNull(uploadFileRequest, "Parameter 'uploadFileRequest' must not be empty.");
        if (this.uploadFileRequests == null) {
            this.uploadFileRequests = new LinkedList();
        }

        this.uploadFileRequests.add(uploadFileRequest);
    }

    public static class EntryBuilder {
        private UploadFileRequestBuilder innerBuilder;
        private BatchUploadFileRequests batchBuilder;

        public EntryBuilder(BatchUploadFileRequests batchBuilder, String bucketName, String key) {
            this.batchBuilder = batchBuilder;
            this.innerBuilder = new UploadFileRequestBuilder(bucketName, key);
        }

        public EntryBuilder key(String key) {
            this.innerBuilder.key(key);
            return this;
        }

        public EntryBuilder taskNum(int taskNum) {
            this.innerBuilder.taskNum(taskNum);
            return this;
        }

        public EntryBuilder uploadFile(String uploadFile) {
            this.innerBuilder.uploadFile(uploadFile);
            return this;
        }

        public EntryBuilder enableCheckpoint(boolean enableCheckpoint) {
            this.innerBuilder.enableCheckpoint(enableCheckpoint);
            return this;
        }

        public EntryBuilder checkpointFile(String checkpointFile) {
            this.innerBuilder.checkpointFile(checkpointFile);
            return this;
        }

        public EntryBuilder objectMetadata(ObjectMetadata objectMetadata) {
            this.innerBuilder.objectMetadata(objectMetadata);
            return this;
        }

        public EntryBuilder callback(Callback callback) {
            this.innerBuilder.callback(callback);
            return this;
        }

        public EntryBuilder with(String key) {
            this.addEntry();
            return new EntryBuilder(this.batchBuilder, this.batchBuilder.bucketName, key);
        }

        public BatchUploadFileRequests end() {
            this.addEntry();
            return this.batchBuilder;
        }

        public List<UploadFileRequest> endAndGet() {
            return this.end().get();
        }

        private void addEntry() {
            this.batchBuilder.add(this.innerBuilder.build());
        }
    }
}
